package com.tianyalei.wolf.sample.activity.collapsing;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;

import com.tianyalei.wolf.mddesignlib.adater.RecyclerAdapter;
import com.tianyalei.wolf.mddesignlib.base.RefreshPresenter;
import com.tianyalei.wolf.mddesignlib.base.listener.FreshAndLoadListener;
import com.tianyalei.wolf.mddesignlib.base.page.PageBean;
import com.tianyalei.wolf.mddesignlib.utils.ToastUtil;
import com.tianyalei.wolf.mddesignlib.view.autorecyclerview.AutoLoadRecyclerView;
import com.tianyalei.wolf.mddesignlib.view.loading.RotateLoading;
import com.tianyalei.wolf.sample.R;
import com.zhy.base.adapter.ViewHolder;
import com.zhy.base.adapter.recyclerview.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 滑动时差效果
 */
public class CollapsingToolbarLayoutActivity extends AppCompatActivity implements FreshAndLoadListener {
    @Bind(R.id.recyclerView)
    AutoLoadRecyclerView mRecyclerView;
    @Bind(R.id.toolbar1)
    Toolbar mToolbar;
    @Bind(R.id.collapsing_toolbar)
    CollapsingToolbarLayout mCollapsingToolbarLayout;

    private RefreshPresenter<String> refreshPresenter;

    @Bind(R.id.loading)
    RotateLoading loading;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collapsing_toolbar_layout);
        ButterKnife.bind(this);

        initData();

        initToolBar();

        //使用CollapsingToolbarLayout必须把title设置到CollapsingToolbarLayout上，设置到Toolbar上则不会显示
        mCollapsingToolbarLayout.setTitle("CollapsingToolbarLayout");
        //通过CollapsingToolbarLayout修改字体颜色
        mCollapsingToolbarLayout.setExpandedTitleColor(Color.WHITE);//设置还没收缩时状态下字体颜色
        mCollapsingToolbarLayout.setCollapsedTitleTextColor(Color.GREEN);//设置收缩后Toolbar上字体的颜色
        //如果不需要title，就用下面的方法
//        mCollapsingToolbarLayout.setTitleEnabled(false);

        refreshPresenter = new RefreshPresenter<>(mRecyclerView);
        refreshPresenter.setFreshAndLoadListener(this);
        refreshPresenter.loadFirst();
    }

    private void initToolBar() {
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    private void initData() {
        mRecyclerView.setAdapter(new RecyclerAdapter<String>(this, new ArrayList<String>(), R.layout.layout_item) {
            @Override
            public void convert(ViewHolder holder, String s) {
                holder.setText(R.id.item_tv, s);
            }
        });

        mRecyclerView.setOnItemClickLitener(new OnItemClickListener() {
            @Override
            public void onItemClick(ViewGroup parent, View view, Object o, int position) {
                ToastUtil.showToast(CollapsingToolbarLayoutActivity.this, position + "");
            }

            @Override
            public boolean onItemLongClick(ViewGroup parent, View view, Object o, int position) {
                ToastUtil.showToast(CollapsingToolbarLayoutActivity.this, position + "long");
                return false;
            }
        });
    }

    @Override
    public void fresh() {
        loading.start();

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                List<String> datas = new ArrayList<>();
                for (int i = 0; i < 15; i++) {
                    datas.add(i + "");
                }
                refreshPresenter.freshFinish(datas);
                loading.stop();
            }
        }, 2000);

    }

    @Override
    public void loadMore(PageBean pageBean) {
        List<String> datas = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            datas.add(pageBean.getOnePageSize() * pageBean.getPageNum() + i + "");
        }
        refreshPresenter.loadFinish(datas);
    }
}
